<template>
  <div>
    <div class="crumbs">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>
          <i class="el-icon-lx-cascades"></i>
          合同管理
        </el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="container">
      <div class="handle-box">
        <el-select v-model="ser.type" class="handle-select mr10">
          <el-option :key="0" label="全部" :value="0"></el-option>
          <el-option :key="it.id" :label="it.name" :value="it.id" v-for="it in types"></el-option>
          <template #prefix>合同类型</template>
        </el-select>
        <el-input v-model="ser.key" placeholder="项目名称" style="width: 260px" class="handle-input mr10">
          <template #prepend>项目名称</template>
        </el-input>
        <el-date-picker v-model="ser.start" type="date" placeholder="开始日期" class="handle-input mr10" value-format="YYYY-MM-DD"></el-date-picker>
        <el-date-picker v-model="ser.end" type="date" placeholder="结束日期" class="handle-input mr10" value-format="YYYY-MM-DD"></el-date-picker>
        <el-button type="primary" icon="el-icon-search" @click="searchClick">搜索</el-button>
        <el-button @click="addClick">添加</el-button>
      </div>
      <el-table :data="datas" border class="table" ref="multipleTable" header-cell-class-name="table-header">
        <el-table-column prop="typeName" label="合同类型" width="100"></el-table-column>
        <el-table-column prop="code" label="合同编号" width="230"></el-table-column>
        <el-table-column prop="desc" label="描述"></el-table-column>
        <el-table-column prop="projectName" label="项目名称"></el-table-column>
        <el-table-column prop="customerName" label="客户名称" v-if="bigScreen"></el-table-column>
        <el-table-column prop="money" label="合同金额" v-if="bigScreen" width="120" :formatter="(...arr) => arr[2] + '元'"></el-table-column>
        <el-table-column prop="orgName" label="部门名称" width="120"></el-table-column>
        <el-table-column prop="salerName" label="销售人员" width="120"></el-table-column>
        <el-table-column prop="signTime" label="签订时间" v-if="bigScreen" :formatter="dayformatter" width="100"></el-table-column>
        <el-table-column label="操作" width="160" align="center">
          <template #default="scope">
            <el-button type="text" icon="el-icon-edit" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button type="text" icon="el-icon-delete" class="red" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination background layout="total, prev, pager, next" :current-page="ser.page" :page-size="ser.row" :total="total" @current-change="handlePageChange"></el-pagination>
      </div>
    </div>
    <form-add ref="r_add" @search="search"></form-add>
  </div>
</template>
<script>
import formAdd from './Add.vue'
export default {
  components: { formAdd },
  data() {
    return {
      ser: {
        type: 0,
        key: '',
        start: '',
        end: '',
        page: 1,
        row: 15,
      },
      datas: [],
      total: 0,
      bigScreen: window.innerWidth > 768,
    }
  },
  methods: {
    searchClick() {
      this.ser.page = 1
      this.search()
    },
    search() {
      this.$api.contract.search(this.ser).then(({ msg: { items, total } }) => {
        this.total = total
        this.datas = items
      })
    },
    addClick() {
      this.$refs.r_add.showadd()
    },
    handleEdit(row) {
      this.$refs.r_add.showadd(row)
    },
    handleDelete(row) {
      this.$confirm('确定要删除当前合同吗？').then(() => {
        this.$api.contract.remove(row.id).then(({ state, msg }) => {
          this.$message[state ? 'success' : 'warning'](msg)
          if (state) {
            this.search()
          }
        })
      })
    },
    handlePageChange(page) {
      this.ser.page = page
      this.search()
    },
    dayformatter(...arr) {
      return arr[2].slice(0, 10)
    },
  },
  mounted() {
    this.searchClick()
  },
}
</script>
<style lang="scss" scoped></style>
